package com.zrrd.yt.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.zrrd.yt.item.service.ItemService;
import com.zrrd.yt.list.client.ListFeignClient;
import com.zrrd.yt.model.product.BaseCategoryView;
import com.zrrd.yt.model.product.SkuInfo;
import com.zrrd.yt.model.product.SpuSaleAttr;
import com.zrrd.yt.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author HongyueYang
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getProductInfo(Long skuId) {
        //创建最终返回的Map
        Map<String, Object> map = new HashMap<>();

        //获取sku基本信息
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(()->{
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);


        //获取sku分类信息
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        },threadPoolExecutor);


        //获取sku价格
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
                map.put("price", skuPrice);
            }
        },threadPoolExecutor);


        //获取属性信息
        CompletableFuture<Void> spuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrs = productFeignClient.getSpuSaleAttrListBySku(skuInfo.getSpuId(), skuId);
            map.put("spuSaleAttrList", spuSaleAttrs);
        },threadPoolExecutor);


        //获取切换商品的json数据
        CompletableFuture<Void> valueMapCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map valueMap = productFeignClient.selectSkuAttrValueBySpu(skuInfo.getSpuId());
            String valuesJson = JSON.toJSONString(valueMap);
            System.out.println("汇总数据获取的属性JSON===>" + valuesJson);
            map.put("valuesSkuJson", valuesJson);
        },threadPoolExecutor);

        //更新商品热度
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.updateHotScore(skuId);
        },threadPoolExecutor);

        //创建多任务组合并且执行
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                skuPriceCompletableFuture,
                spuSaleAttrCompletableFuture,
                valueMapCompletableFuture,
                voidCompletableFuture
        ).join();

        //返回map
        return map;
    }
}
